package org.lan.until;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.lan.entity.User;

import java.util.Date;

public class PasswordHelper {

    private String algorithmName = "md5";

    private int hashIterations = 5;

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    public void encryptPassword(User user) {
        String salt = "" + new Date();
        user.setSalt(ByteSource.Util.bytes(salt).toHex());
        String newPassword = new SimpleHash(algorithmName, user.getPassword(), salt, hashIterations).toHex();
        user.setPassword(newPassword);
    }
}
